import 'package:flutter/material.dart';

import '../../utils/screenutil/fit.dart';

// 小区列表数据类型（含全部，不含全部）
enum DROPDOWNTYPE { ALL, NON }

class CommunityListBean {
  static List<CommunityBean> _list;

  // 小区列表（下拉框数据）
  static List<DropdownMenuItem<CommunityBean>> dropDownData = [];
  static List<DropdownMenuItem<CommunityBean>> dropDownDataAll = [];

  // -
  CommunityListBean.fromJson(List json) {
    List<CommunityBean> newList = new List();
    if (json != null) {
      json.forEach((v) => newList.add(CommunityBean.fromJson(v)));
      _list = newList;
    }
  }

  /// 创建小区集合（含全部）
  static List<DropdownMenuItem<CommunityBean>> getCommunityAll() {
    if (dropDownDataAll.length <= 0) {
      dropDownDataAll.insert(
        0,
        DropdownMenuItem<CommunityBean>(
          child: Text("全部", style: TextStyle(fontSize: 24.f)),
          value: CommunityBean.fromJson({"id": "", "name": "全部", "type": null}),
        ),
      );

      // 创建下拉框数据
      _createCommunity(dropDownDataAll);
    }

    // 返回小区集合（含全部）
    return dropDownDataAll;
  }

  /// 创建小区集合（不含全部）
  static List<DropdownMenuItem<CommunityBean>> getCommunity() {
    // 创建下拉框数据
    if (dropDownData.length <= 0) _createCommunity(dropDownData);
    return dropDownData;
  }

  /*
  /// 创建小区集合
  static List<DropdownMenuItem<CommunityBean>> getCommunity(
      {DROPDOWNTYPE type}) {

    if (type == DROPDOWNTYPE.ALL) {
      if (dropDownDataAll.length <= 0) {
        dropDownDataAll.insert(
          0,
          DropdownMenuItem<CommunityBean>(
            child: Text("全部", style: TextStyle(fontSize: 24.f)),
            value:
                CommunityBean.fromJson({"id": "", "name": "全部", "type": null}),
          ),
        );

        // 创建下拉框数据
        _createCommunity(dropDownDataAll);
      }

      return dropDownDataAll;
    } else {
      if (dropDownData.length <= 0) {
        // 创建下拉框数据
        _createCommunity(dropDownData);
      }
      return dropDownData;
    }
  }
  */

  /// 根据病区数据创建下拉列表
  /// * [data] List<DropdownMenuItem<CommunityBean>>|必填
  static _createCommunity(List<DropdownMenuItem<CommunityBean>> data) {
    // 创建DropdownMenuItem内容
    for (int i = 0; i < _list.length; i++) {
      data.add(
        DropdownMenuItem<CommunityBean>(
          child: Text("${_list[i].name}", style: TextStyle(fontSize: 24.f)),
          value: _list[i],
        ),
      );
    }
    return data;
  }

  /// 单例
  CommunityListBean._();
  static CommunityListBean _currentCommunityBean = CommunityListBean._();
  static CommunityListBean getInstance() => _currentCommunityBean;

  // 向外暴露访问途径
  static List<CommunityBean> get list => _list;
}

// -
class CommunityBean {
  String id;
  String name;

  CommunityBean({this.id, this.name});

  CommunityBean.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }
}
